package org.opu.pc.sg;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import org.opu.pc.util.DateUtil;
import org.apache.commons.io.IOUtils;
import org.opu.pc.ui.util.Logger;

/**
 *
 * @author Yuriy Dunko
 */
public class StatementGenerationServicesImpl implements StatementGenerationServices {

    private Logger logger = Logger.create(StatementGenerationServicesImpl.class);

    private AbstractStatementGenerator st1;
    private AbstractStatementGenerator st4;
    private AbstractStatementGenerator st5;

    public StatementGenerationServicesImpl(AbstractStatementGenerator st1,
            AbstractStatementGenerator st4, AbstractStatementGenerator st5) {
        this.st1 = st1;
        this.st4 = st4;
        this.st5 = st5;
    }
    
    @Override
    public void departmentPublicationsStatement(Date begin, Date end,
            OutputStream output) throws IOException {
        int y1 = DateUtil.getYear(begin);
        int y2 = DateUtil.getYear(end);

        logger.info("departmentPublicationsStatement: start prepare statement: "
                + y1 + " - " + y2);

        st1.resetSGProperties("/org/opu/pc/sg/st1_properties.properties");
        st1.setSGProperty(St1StatementGenerator.BEGIN_YEAR_VAR, String.valueOf(y1));
        st1.setSGProperty(St1StatementGenerator.END_YEAR_VAR, y1 != y2 ? "- " + y2 : "");
        
        st1.resetVariables();
        st1.addVariable(St1StatementGenerator.BEGIN_YEAR_VAR, begin);
        st1.addVariable(St1StatementGenerator.END_YEAR_VAR, end);

        try {
            IOUtils.write(st1.generateDocument(), output);
        } finally {
            IOUtils.closeQuietly(output);
        }
    }

    @Override
    public void studentScienceStatement(Date begin, Date end,
            OutputStream output) throws IOException {
        try {
            IOUtils.write(st4.generateDocument(), output);
        } finally {
            IOUtils.closeQuietly(output);
        }
    }

    @Override
    public void professorScienceStatement(Date begin, Date end,
            Integer professorId, OutputStream output) throws IOException {
        try {
            IOUtils.write(st5.generateDocument(), output);
        } finally {
            IOUtils.closeQuietly(output);
        }
    }
}
